<?php
 mb_internal_encoding("utf-8");
 //gc_enabled();
 require "../document/class_corpus.php";
 require "../document/class_document.php";
 require "../document/class_document_xml.php";

 require "../files/tool_files_dirs.php";

 require "../misc/class_tag.php";
 require "../misc/tool_color.php";

 require "../tree/class_tree.php";
 require "../tree/class_tree_xhtml.php";
 require "../tree/class_structure.php";
 require "../tree/tool_tree.php";

 require "../array/tool_array.php";

 require "../pear_graph/tool_pear_graph.php";

 require "../maths/tool_maths.php";

 require "../string/tool_string.php";
 require "../string/class_segment.php";
 require "../string/class_suffixarray_new.php";
 require "../string/suffix_array.php";

 require "../pdo/class_sql_document.php";

//$dir="../../xml/europa_complete_repair/";
//$dir="../xml/europa_test/";
//$dir="../xml/europa_test2/";
//$dir="../xml/test/";

//$lg_candidates=array("cs","da","de","el","en","es","et","fi","fr","hu","it","lt","lv","nl","pl","pt","sl","sw");
$lg_candidates=array("fr","en","da");

//$lg_candidates=array("fr");
//$array_ids_europa=tool_corpus::get_unified_europa($dir,$lg_candidates);
//$motif_lg=implode("|",$lg_candidates);
$motif_lg="fr";
$lg="fr";



function complete_mt($_mt_vect,$_data){
  foreach($_data as $d){
    list($id_bloc,$nb_occ)=explode(":",$d);
    $_mt_vect[$id_bloc]=$nb_occ;    
  }
  return $_mt_vect;
}

function complete_mt2($_mt_vect,$_data){
  foreach($_data as $id_grain=>$d){
    $_mt_vect[$id_grain]=$d;
  }
  return $_mt_vect;
}

$start=microtime(true);
//  $exp=new sql_experience('align_rstr_exp01');
//  $exp=new sql_experience('align_word_exp02');
  $exp=new sql_experience('align_debug');
  $array_obj_sql=$exp->reader_experience();
  $global_array_corres=array();
  $res_all_count_bloc=array();
  foreach($lg_candidates as $lg){
    $array_corres=$array_obj_sql["sql_rstr"]->reader_all2relative_id_bloc($lg);
    $global_array_corres[]=$array_corres;
    $array_obj_sql["sql_rstr"]->reader_all2count_bloc2($res_all_count_bloc,$lg,$array_corres);
  }
$end=microtime(true)-$start;
echo "\n total1 :: $end\n";

$start=microtime(true);
  $mt_vector=array_fill(1,count(reset($array_corres)),0);
  $base_vector=array_keys($mt_vector);
  $count_vector=count($base_vector);
  $data_tmp=array();
  $array_request=$array_data=array();
  $cpt=$cpt2=0;

  foreach($res_all_count_bloc as $rep=>$array_occ){
    $array_req = reset($array_occ);
    $array_req[2] = 1;
    $info_src=array(
      $array_obj_sql["sql_groupe"]->reader_id_graphie_lg_type2id_groupe($array_req),
      complete_mt($mt_vector,explode("|",$rep))
    );
    $array_data=array();
    foreach($array_request as $info_target){
//      print_r($info_src[1]);
//      print_r($info_target[1]);
//      die();
      $angle=1-(tool_maths::get_angle($info_src[1],$info_target[1],$base_vector,$count_vector));
      $jac=tool_maths::get_jaccard($info_src[1],$info_target[1],$base_vector,$count_vector);
      $im=tool_maths::get_IM($info_src[1],$info_target[1],$base_vector,$count_vector);
      $array_data[]=array(
        "id_groupe1"=>$info_src[0],
        "id_groupe2"=>$info_target[0],
        "cos"=>$angle,
        "jac"=>$jac,
        "im"=>$im
      );
    }
    if($array_data)
      $array_obj_sql["sql_comparaison"]->writer_array_values($array_data);
    unset($array_data);
    echo "\r";
    echo ++$cpt2;
    $array_request[]=$info_src;
  }
  unset($res_all_count_bloc);
$end=microtime(true)-$start;
echo "\n total2 :: $end\n";
die();
//print_r(array_keys($data_tmp));
//die();

//$start=microtime(true);
//  $res2=$array_obj_sql["sql_groupe"]->reader_all2array_id_group_id_obj();
//$end=microtime(true)-$start;
//echo "\n total3 :: $end\n";

//print_r($res2);

$start=microtime(true);

  $cpt2=0;
  $start=microtime(true);
  $array_data=array();
  $count=count($data_request);
  while($data_request){
    $start=microtime(true);
    $cpt2++;
    echo "\r $cpt2 / $count";
    $info_src=array_shift($data_request);
//    $key_src=$info_src[0];
//    $id_group_src=$info_src[0];
//    $rep_src=$info_src[1];

    foreach($data_request as $info_target){
//      $key_target=$info_target[0];
//      $id_group_target=$info_target[0];
//      $rep_target=$info_target[1];
//      print_r($info_src[1]);
//      print_r($info_target[1]);
      $angle=1-(tool_maths::get_angle($info_src[1],$info_target[1],$base_vector,$count_vector));
//      echo "==> $angle";
//      die();

      $array_data[]=array(
        "id_groupe1"=>$info_src[0], "id_groupe2"=>$info_target[0],
        "methode"=>"cosinus",
        "score"=>$angle
      );

      $array_data[]=array(
        "id_groupe1"=>$info_target[0], "id_groupe2"=>$info_src[0],
        "methode"=>"cosinus",
        "score"=>$angle
      );
//      print_r($array_data);
//      die();
    }
  //  $array_obj_sql2["comparaison"]->writer_array_values($array_data);
    unset($array_data);
    $array_data=array(); 
    $end=microtime(true)-$start;
//    echo "\n total4 :: $end\n";
//    die();
  }

$end=microtime(true)-$start;
echo "\n total4 :: $end\n";

die();

//  $exp2=new sqlite_experience("./sqlite_comparaison.sdb");
/*
foreach($res2 as $occ){
//  echo $occ[0].'=>'.$occ[2].':'.$occ[1]."\n";
  if(!isset($data_tmp["$occ[2]:$occ[1]"])){
    print_r($occ);
    die();
  }
}
if(count($res2)==count($data_tmp)) echo "yeah!";
else echo "bordel, quoi encore ?!";
die();
*/
die();
/*
foreach($res2 as $info){
  $id_lg=$info[2];
//  print_r($info);
//  die();
  $id_graphie=$info[1];
  $rep=$array_obj_sql[$id_lg]["rstr"]->reader_id2count_bloc($id_graphie);
  $repartition=complete_mt2($mt_vector,$rep);
  print_r($repartition);
  die();
}
*/
die();

$array_lg_count=array();
$array=array();
$array_data=array();
$res=array();
$start=microtime(true);
foreach($lg_candidates as $lg){
  $exp=new sqlite_experience("./sdb/word/sqlite_document.$lg.sdb");
  $array_obj_sql=$exp->reader_experience();
  $array_obj_sql["rstr"]->reader_all2count_bloc2($res,$lg);
  $array_lg_count[$lg]=count($array_obj_sql["graphie"]->reader_all2array());
}

$end=microtime(true)-$start;
echo "\n total1 :: $end\n";

$exp2=new sqlite_experience("./sqlite_comparaison.sdb");
$array_obj_sql2=$exp2->reader_experience();

$array_data=array();
$id_groupe=0;

$start=microtime(true);
foreach($res as $reparition=>$array_occ){
  ++$id_groupe;
  foreach($array_occ as $occ){
    $array_data[]=array(
      "id_groupe"=>$id_groupe,
      "type_group"=>"standard",
      "type_obj"=>"word",
      "id_obj"=>$occ[1],
      "id_lg"=>$occ[2]
    );
  }

  if(($id_groupe%500)==0){
    $array_obj_sql2["groupe"]->writer_array_values($array_data);
    unset($array_data);
    $array_data=array();
    ++$cpt2;
  }

  echo "\r$cpt2-$id_groupe";
}
$end=microtime(true)-$start;
echo "\n total2 :: $end\n";
die();
/*
  print_r($array_graphie);
  die();
   
  while($array_graphie){
    $occ=array_shift($array_graphie);
    foreach($array_graphie as $occ_comp){
      $array_data[]=array(
        "type"=>"word",
        "id_graphie1"=>$occ[1], "id_lg1"=>$occ[2],
        "id_graphie2"=>$occ_comp[1], "id_lg2"=>$occ_comp[2],
        "methode"=>"cosinus",
        "score"=>0
      );
      $array_data[]=array(
        "type"=>"word",
        "id_graphie1"=>$occ_comp[1], "id_lg1"=>$occ_comp[2],
        "id_graphie2"=>$occ[1], "id_lg2"=>$occ[2],
        "methode"=>"cosinus",
        "score"=>0
      );
    }
    if($cpt>500){
      $array_obj_sql2["comparaison"]->writer_array_values($array_data);
      unset($array_data);
      $array_data=array();
      $cpt=0;
      ++$cpt2;
    }
    else{
      ++$cpt;
    }
//    ++$cpt2;
    echo "\r$cpt2-$cpt";
  }
}

*/

//print_r($array_data);
die();

print_r($array_data);
die();

$array_data=array();
foreach($array as $ab=>$array_group_lg){
  while($array_group_lg){
    $lg1=key($array_group_lg);
    $group1=array_shift($array_group_lg);
    print_r($group1);
//    die();
//    foreach($group1 as $info1){
//    while($group1){
//      $info1=array_unshift($group1);
/*
      foreach($group1 as $info11){
          $array_data[]=array(
            "type"=>"word",
            "id_graphie1"=>$info1[1],
            "lg1"=>$lg1,
            "id_graphie2"=>$info11[1],
            "lg2"=>$lg1,
            "methode"=>"cosinus",
            "score"=>1
          );
      }
*/
/*
      foreach($array_group_lg as $lg2=>$group2){
        foreach($group2 as $info2){
          $array_data[]=array(
            "type"=>"word",
            "id_graphie1"=>$info1[1],
            "lg1"=>$lg1,
            "id_graphie2"=>$info2[1],
            "lg2"=>$lg2,
            "methode"=>"cosinus",
            "score"=>1
          );
        }
      }
*/
//    }
  }
}
      print_r($array_data);
      die();


die();


$cpt=0;
$res="";
while(count($array)>0){
  $k=key($array);
  $data=array_shift($array);
  $k=explode("|",$k);
  $mt=complete_mt($mt_vector,$k);
//  print_r($data);
  $array_data=array();
  foreach($data as $lg=>$array_id_graphie){
    $id_graphie=array_shift($array_id_graphie);
    foreach($array_id_graphie as $id_graphie_comp){

    }
  }

  die();
  $array_data=array(
    "type"=>"word",
    "id_graphie1"=>""
  );

  foreach($array as $ab=>$group_lg){
    $comp_k=explode("|",$ab);
    $compt_mt=complete_mt($mt_vector,$comp_k);
    $jaccard=tool_maths::get_jaccard($mt,$compt_mt);
    $angle=tool_maths::get_angle($mt,$compt_mt);

    die();
//    print_r($comp_k);
//    die();
  }

  die();
}
die();


foreach($array as $ab=>$group_lg){
//  print_r($ab);
//  print_r($group_lg);
//  if($cpt>4)die();
//  $cpt++;
/*
  foreach($group_lg as $lg=>$array_rstr){
    foreach($array_rstr as $array_info){
//      print_r($array_info);
//      die();
      $array_lg_id[$lg][]=$array_info[1];
    }
  }
*/
}

print_r($array_lg_id);

die();



/*
foreach($lg_candidates as $lg){
  $connexion_pdo=new PDO("sqlite:./sdb/sqlite_document.$lg.sdb");
  $connexion_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $requete='
    CREATE INDEX indexRstr
    on Rstr (id_bloc,id_graphie)
  ;';
  $prepare_execute = $connexion_pdo->prepare($requete,array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
  $prepare_execute->execute();
  $connexion_pdo = null;
}
$end=microtime(true)-$start;
//print_r($res);
echo "\n total :: $end\n";
die();

die();
*/

//print_r($array_bloc_parent);
//die();
$array_lg_count=array();
$start=microtime(true);
$array=array();
foreach($lg_candidates as $lg){
//if($lg="fr"){
//  echo $lg." ";
  $exp=new sqlite_experience("./sdb/rstr/sqlite_document.$lg.sdb");
  $array_obj_sql=$exp->reader_experience();
  $res=$array_obj_sql["rstr"]->reader_all2count_bloc($lg);
  $array_lg_count[$lg]=count($array_obj_sql["graphie"]->reader_all2array());
  foreach($res as $ab=>$group_lg){
    $array[$ab][$lg]=$group_lg;
  }
}
//echo "\n";
//echo "generation : ";
//echo count($array);
//echo "\n";

foreach($array as $ab=>$group_lg){
  if(count($group_lg)!=18){
    unset($array[$ab]);
  }
}

/*
echo "filtrage : ";
echo count($array);
echo "\n";
echo "\n";
echo "\n";
echo "\n";
*/
$array_lg_id=array();

foreach($array as $ab=>$group_lg){
  foreach($group_lg as $lg=>$array_rstr){
    foreach($array_rstr as $array_info){
      $array_lg_id[$lg][]=$array_info[1];
    }
  }
}

foreach($array_lg_id as $lg=>$array_id){
 echo $lg."\n  ".count($array_id)."\n  ".$array_lg_count[$lg]."\n"; 
}

//$end=microtime(true)-$start;
//echo "\n total :: $end\n";
die();

die();

echo "\n";

echo "\n";
echo count($array_repartition);
echo "\n";
die();

$clique=array();
$res_group=array();
foreach($res as $i=>$graphie){
  echo $i."\r";
  $g=$graphie["graphie"];
//  $all=$array_obj_sql["rstr"]->reader_graphie2array($g,"","ORDER BY id_bloc");

  $simple_clique=$array_obj_sql["rstr"]->reader_graphie2count_bloc($g,"","ORDER BY id_bloc");
/*
  $simple_clique=array();
  foreach($all as $occurence){
    $simple_clique[$occurence[2]]++;
  }
*/
/*
  $search=array_search($simple_clique,$clique);
  if($search!==false){
    $res_group[$search][]=$g;
  }
  else{
    $id_clique=array_push($clique,$simple_clique)-1;
    $res_group[$id_clique]=array($g);
  }
*/
}
/*
foreach($res_group as $group){
  foreach($group as $graphie){
    echo "$graphie ";
  }
  echo "\n\n\n";
}
*/
$end=microtime(true)-$start;
//print_r($res);
echo "\n total :: $end\n";
die();

//print_r(count($res));
die();

die();
//$array_ids_europa=tool_corpus::europa_get_all_ids($dir);
$array_corpus_groupe=array();
$cpt=0;
$taille=0;
$array_bloc_str=array();
$factory_rstr=new suffixarray();
$array_bloc_doc=array();
//$factory_mot=new factory_mot();

$res=$array_obj_sql['bloc']->reader_id_document2array(1);
print_r($res);
die();

$start=microtime(true);
$connexion_pdo=new PDO("sqlite:sqlite_document.v1.sdb");
//$connexion->
$connexion_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$requete='
  SELECT b.id_bloc,
  FROM `Rstr` r, `Bloc` b, `Graphie` g
  WHERE g.id_graphe=r.id_graphie
    AND ;';
$prepare_execute = $connexion_pdo->prepare($requete,array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$tab_valeurs=array(':id_document'=>$_id_document);
$prepare_execute->execute($tab_valeurs);
foreach($prepare_execute as $ligne){
  $array_res[]=array($ligne["id_bloc"],$ligne["contenu"]);
}
$connexion_pdo = null; //Appel au destructeur de l'objet PDO
print($array_res);
die();

//$res=$array_obj_sql['bloc']->reader_id_document2array(1);
//print_r($res);
/*
//$res=$array_obj_sql["rstr"]->reader_all2array();
$array_bloc=$array_obj_sql['bloc']->reader_all2array();
$res=$array_obj_sql['rstr']->reader_graphie2array("été","","ORDER BY id_bloc,offset");
foreach($res as $seg){
  $bloc=$array_bloc[$seg[2]-1];
//  print_r($bloc);
  echo mb_substr($bloc[2],$seg[0],$seg[1]);
//  echo $bloc;
  echo "\n";
}
//print_r($res);
*/
$end=microtime(true)-$start;
//print_r($res);
echo "\n total :: $end\n";
die();

/*
$array_obj_sql=array(
  "document"=>new sqlite_document(),
  "bloc"=>new sqlite_bloc(),
  "rstr"=>new sqlite_rstr()
); 
$array_obj_sqlite=$exp->writer_create_experience($array_obj_sql);
*/
//$array_obj_sql=$experience->reader_experience();
//$experience->writer_drop_create_experience($array_obj_sql);

//print_r($array_obj_sql);
//die();
//$array_obj_sql=$experience->reader_experience();

foreach($array_ids_europa as $id_europa){
  $corpus=tool_corpus::get_corpus_regexp($dir,$id_europa."($motif_lg)");
  $array_doc=$corpus->__get_array_doc();
  foreach($array_doc as $document){
    $id_doc=mb_strcut($document->__get_id(),-6,2);
    $path=$document->__get_id();
    $structure=$document->__get_structure();
    $signature=$structure->__get_signature();
    tool_tree::cleanstructure($signature);
    $signature->__recalc_idn();

    $test=new RecursiveIteratorIterator($signature,RecursiveIteratorIterator::SELF_FIRST);
    $array_new_doc=array("path_doc"=>$path, "lg"=> $id_doc);
    $id_doc=$array_obj_sql["document"]->writer_values($array_new_doc);

    foreach($test as $id_bloc=>$bloc_xhtml){
      $i=array_push($array_bloc_str,tool_tree::tree_xhtml2texte($bloc_xhtml->__get_value()))-1;
      $array_new_bloc=array(
        "id_document"=>$id_doc,
        "contenu"=> $array_bloc_str[$i]
      );
      $id_bloc=$array_obj_sql["bloc"]->writer_values($array_new_bloc);
      $factory_rstr->__add_str($array_bloc_str[$i],$id_bloc);
//      $array_doc_bloc[$id_doc][]=$id_bloc;
//      $array_bloc_doc[$id_bloc]=$id_doc;
//      $factory_mot->get_all_mots($array_bloc_str[count($array_bloc_str)-1],$id_doc);
    }
  }
}

$all_res=$factory_rstr->__init();
$array_request=array();
$count=count($all_res);
echo $count;
echo "\n";
foreach($all_res as $c=>$rstr){
  $su=$factory_rstr->__get_suffix($rstr[1][0]);
  $global_str=$factory_rstr->__get_str($su[2]);
  $str=mb_substr($global_str,$su[0],$rstr[0]);
  echo $count-$c;
  echo "-";
  echo count($rstr[1]);
  echo "\n";
  foreach($rstr[1] as $pos){
    $su=$factory_rstr->__get_suffix($pos);
    $array_request[]=array(
      "id_bloc"=>$su[2],
      "offset"=>$su[0],
      "len"=>$rstr[0],
      "graphie"=>$str
    );
//    $array_obj_sql["rstr"]->writer_values($array_new_rstr);
  }
}
echo "\n";
echo count($array_request);
echo "\n";
$array_obj_sql["rstr"]->writer_array_values($array_request);
foreach($array_request as $j=>$r){
//  $array_obj_sql["rstr"]->writer_values($r);
//  $nc=$c-$j;
//  echo "\r".$nc;
}
$end=microtime(true)-$start;
echo "\n total :: $end\n";
die();
die();

$zipf=array();

 /**********************
  * $rstr[0]==len
  * $rstr[1]==array_pos
  ***********************/
die();
foreach($all_res as $i=>$rstr){
  $su=$factory_rstr->__get_suffix($rstr[1][0]);
  $global_str=$factory_rstr->__get_str($su[2]);
  $str=mb_substr($global_str,$su[0],$rstr[0]);
  foreach($rstr[1] as $pos){
    $su=$factory_rstr->__get_suffix($pos);
    $id_doc=$array_bloc_doc[$su[2]];
    $zipf[$id_doc][$str]++;
  }
}

/*
 foreach($zipf as $id_doc=>$zipf_d){
   arsort($zipf_d);
   echo "id_doc : $id_doc**************************\n";
   foreach($zipf_d as $str=>$cpt){
     echo "[$cpt]\n    $str\n";
   }   
 }

die();
*/
function fill_graph(&$graph,$data_x,$data_y){
  fill_graph_aux($graph,$data_x,$data_y);
  return $graph->plot04();
}

function fill_graph_aux(&$graph,$data_x,$data_y){
  $graph->__add_array_x($data_x);
  $graph->__add_array_y($data_y);
}

$tool_graph=new Tool_pear_graph();
$list_ids_doc="";
foreach($zipf as $id_doc => $res_zipf){
 $list_ids_doc.="[$id_doc]";
 $zipf=tool_maths::zipf($res_zipf);
 $zipf_log=tool_maths::zipf_log($zipf);
 $x=array_keys($zipf_log);
 $y=array_values($zipf_log);
 $regression=tool_maths::regression_lineaire_simple($x,$y);

 $first_x=reset($x);
 $first_y=$regression["a"]*$first_x+$regression["b"];
 $last_x=end($x);
 $last_y=$regression["a"]*$last_x+$regression["b"];

 $tool_graph->__add_plot_title("coeff. $id_doc : $regression[r]");    
 fill_graph_aux($tool_graph,array($first_x,$last_x),array($first_y,$last_y));
// $tool_graph->__add_array_x(array($first_x,$last_x));
// $tool_graph->__add_array_y(array($first_y,$last_y));
}

$tool_graph->__add_title("regression linéaire rstr - zipf");
$graph=$tool_graph->line01();
ob_start();
$graph->done();
$graph_svg=ob_get_contents();
Tool_files::file_write("./zipf_rstr_log_regression".$list_ids_doc.".svg",$graph_svg);
ob_clean();
die();

//$factory_mot->init_etiquetage();
//$start=microtime(true);
//$factory_mot->init_chunk();
//$end=microtime(true)-$start;
//echo "\n total :: $end";
//die();


//$repartition_chunk=$factory_mot->__get_repartiton_chunk();
$repartition_segment=$factory_mot->__get_repartition_segment();
//print_r($repartition_segment);
//die();
$res_all=$zipf=$res_type=array();
$res_type["p"]=$res_type["v"]=$res_type["i"]=0;
foreach($repartition_segment as $id_doc=>$array_bloc){
  foreach($array_bloc as $id_bloc=>$array_id_segment){
    foreach($array_id_segment as $id_segment){
      $mot=$factory_mot->__get_word($id_segment);
      $len=$mot->__get_len();
      $str=mb_strtolower($mot->__tostring());
      $type=$mot->__get_attribute("type");
      $id_doc=mb_strcut($id_doc,-6,2);
//      $res_all[$id_doc][$len][$str]++;
//      $res_all_type[$id_doc][$len][$str][$type]++;
//      $res_type[$id_doc][$type][$len][$str]++
//      $res_type[$id_doc][$type][$len]++;
//      if($type=="v") $res_v[$len]++;
//      if($type=="p") $res_p[$len]++;
//      if($type=="i") $res_i[$len]++;
//      $res_all[$id_doc][$len]++;
//      if($len>60) echo $mot."\n";
      $zipf[$id_doc][$str]++;
      if($type=="i") $zipf_i[$id_doc][$str]++;
      if($type=="v") $zipf_v[$id_doc][$str]++;
      if($type=="p") $zipf_p[$id_doc][$str]++;
    }
  }
}
/*
function fill_graph(&$graph,$data_x,$data_y){
  fill_graph_aux($graph,$data_x,$data_y);
  return $graph->plot04();
}

function fill_graph_aux(&$graph,$data_x,$data_y){
  $graph->__add_array_x($data_x);
  $graph->__add_array_y($data_y);
}
*/
$tool_graph=new Tool_pear_graph();
$list_ids_doc="";
foreach($zipf as $id_doc => $res_zipf){
 $list_ids_doc.="[$id_doc]";
 $zipf=tool_maths::zipf($res_zipf);
 $zipf_log=tool_maths::zipf_log($zipf);
 $x=array_keys($zipf_log);
 $y=array_values($zipf_log);
 $regression=tool_maths::regression_lineaire_simple($x,$y);

 $first_x=reset($x);
 $first_y=$regression["a"]*$first_x+$regression["b"];
 $last_x=end($x);
 $last_y=$regression["a"]*$last_x+$regression["b"];

 $tool_graph->__add_plot_title("coeff. $id_doc : $regression[r]");    
 fill_graph_aux($tool_graph,array($first_x,$last_x),array($first_y,$last_y));
// $tool_graph->__add_array_x(array($first_x,$last_x));
// $tool_graph->__add_array_y(array($first_y,$last_y));
}

$tool_graph->__add_title("regression linéaire - zipf");
$graph=$tool_graph->line01();
ob_start();
$graph->done();
$graph_svg=ob_get_contents();
Tool_files::file_write("./svg_zipf/zipf_log_regression".$list_ids_doc.".svg",$graph_svg);
ob_clean();
die();

$tool_graph=new Tool_pear_graph();
$tool_graph->__add_title("rang - effectif");
foreach($zipf as $id_doc => $res_zipf){
  $res_zipf=tool_maths::own_zipf_etiquette($res_zipf,array("i"=>$zipf_i[$id_doc],"v"=>$zipf_v[$id_doc],"p"=>$zipf_p[$id_doc]));
  $zipf_log=tool_maths::zipf_log2($res_zipf[0]);
  $tool_graph->__add_plot_title("zipf : $id_doc");    
  fill_graph_aux($tool_graph,$zipf_log[0],$zipf_log[1]);
  $graph=$tool_graph->plot04();
  ob_start();
  $graph->done();
  $graph_svg=ob_get_contents();
  Tool_files::file_write("./svg_zipf/zipf.svg",$graph_svg);
  ob_clean();
}
die();
$graph=$tool_graph->plot04();
ob_start();
$graph->done();
$graph_svg=ob_get_contents();
Tool_files::file_write("./svg_zipf/zipf_i.svg",$graph_svg);
ob_clean();
die();
/*
$tool_graph=new Tool_pear_graph();
foreach($zipf as $id_doc => $res_zipf){
  $res_zipf=tool_maths::own_zipf_etiquette($res_zipf,array("i"=>$zipf_i[$id_doc],"v"=>$zipf_v[$id_doc],"p"=>$zipf_p[$id_doc]));
  $zipf_log=tool_maths::zipf_log($res_zipf[0]);
  $tool_graph->__add_array_x(array_keys($zipf_log));
  $tool_graph->__add_array_y(array_values($zipf_log));
  $tool_graph->__add_plot_title("zipf : $id_doc");

  $zipf_log_v=tool_maths::zipf_log($res_zipf[1]["v"]);
  $tool_graph->__add_array_x(array_keys($zipf_log_v));
  $tool_graph->__add_array_y(array_values($zipf_log_v));
  $tool_graph->__add_plot_title("zipf v : $id_doc");
}

$tool_graph->__add_title("rang - longueur");
$graph=$tool_graph->plot04();
ob_start();
$graph->done();
$graph_svg=ob_get_contents();
Tool_files::file_write("./svg_zipf/zipf_v.svg",$graph_svg);
ob_clean();

$tool_graph=new Tool_pear_graph();
foreach($zipf as $id_doc => $res_zipf){
  $res_zipf=tool_maths::own_zipf_etiquette($res_zipf,array("i"=>$zipf_i[$id_doc],"v"=>$zipf_v[$id_doc],"p"=>$zipf_p[$id_doc]));
  $zipf_log=tool_maths::zipf_log($res_zipf[0]);
  $tool_graph->__add_array_x(array_keys($zipf_log));
  $tool_graph->__add_array_y(array_values($zipf_log));
  $tool_graph->__add_plot_title("zipf : $id_doc");

  $zipf_log_p=tool_maths::zipf_log($res_zipf[1]["p"]);
  $tool_graph->__add_array_x(array_keys($zipf_log_p));
  $tool_graph->__add_array_y(array_values($zipf_log_p));
  $tool_graph->__add_plot_title("zipf p : $id_doc");
}

$tool_graph->__add_title("rang - effectif");
$graph=$tool_graph->plot04();
ob_start();
$graph->done();
$graph_svg=ob_get_contents();
Tool_files::file_write("./svg_zipf/zipf_p.svg",$graph_svg);
ob_clean();
*/
/*
foreach($res_all as $id_doc => $array_len_str){
  ksort($array_len_str);
  $array_res=array();
  foreach($array_len_str as $len=>$array_str){
    asort($array_str);
    foreach($array_str as $str=>$nb_occ){
      $array_res[]=$nb_occ;
    }
  }
  $tool_graph->__add_array_x(array_keys($array_res));
  $tool_graph->__add_array_y(array_values($array_res));
  $tool_graph->__add_plot_title("len : $id_doc");
}
*/
/*
foreach($res_all as $id_doc => $len_word){
  ksort($len_word);
  $tool_graph->__add_array_x(array_keys($len_word));
  $tool_graph->__add_array_y(array_values($len_word));
  $tool_graph->__add_plot_title("len_word : $id_doc");
  foreach($res_type[$id_doc] as $type=>$array_res_type){
    ksort($array_res_type);
    $tool_graph->__add_array_x(array_keys($array_res_type));
    $tool_graph->__add_array_y(array_values($array_res_type));
    $tool_graph->__add_plot_title("type : $type - $id_doc");
  }
}
*/
//die();

/*
$len_chunk=array();
$repartition_chunk=$factory_mot->__get_repartition_chunk();
//print_r($repartition_chunk);
//die();
foreach($repartition_chunk as $id_doc=>$array_bloc){
  $id_doc=mb_strcut($id_doc,-6,2);
  foreach($array_bloc as $id_bloc=>$array_chunk){
    foreach($array_chunk as $array_id_segment){
      $len=0; 
      foreach($array_id_segment as $id_segment){
        $mot=$factory_mot->__get_word($id_segment);
        //if($mot->__get_attribute("type")!="v")
          $len+=$mot->__get_len();
      }
      $len_chunk[$id_doc][$len]++;
    }
  }
}
*/
//die();


/*
foreach($len_chunk as $id_doc => $len){
//  if($id_doc=="fr"){
    ksort($len);
    $tool_graph->__add_array_x(array_keys($len));
    $tool_graph->__add_array_y(array_values($len));
    $tool_graph->__add_plot_title("len_chunk : $id_doc");
//  }
}
*/
//die();
//print_r($zipf_log);
//die();
//print_r($zipf);
//die();
//$tool_graph->__add_array_x(array_keys($res_all));
//$tool_graph->__add_array_y(array_values($res_all));
//$tool_graph->__add_array_x(array_keys($zipf));
//$tool_graph->__add_array_y(array_values($zipf));
//$tool_graph->__add_array_x(array_keys($zipf_log_fi));
//$tool_graph->__add_array_y(array_values($zipf_log_fi));
//$tool_graph->__add_plot_title("zipf finnois log*log");
//$tool_graph->__add_plot_title("zipf");


/*
$tool_graph->__add_array_x(array_keys($res_v));
$tool_graph->__add_array_x(array_keys($res_i));
$tool_graph->__add_array_x(array_keys($res_p));
*/
/*
$tool_graph->__add_array_y(array_values($res_v));
$tool_graph->__add_array_y(array_values($res_i));
$tool_graph->__add_array_y(array_values($res_p));
*/
//$tool_graph->__add_title("droite de régression sur le rapport longueur*effectif de la loi de zipf des documents");
//$tool_graph->__add_title("rapport longueur*effectif de la loi de zipf des documents");
?>
